﻿using UnityEngine;
using System.Collections;

public class powerUpSelector : MonoBehaviour {
	
	public float waitTime;
	private int a;
	public AudioClip PickUpSound;
	[HideInInspector]public Character[] players;
	public Character Actor;
	public Character ActedOn;

	void Start(){
        players = FindObjectsOfType<Character>();
/*		var p = GameObject.FindGameObjectsWithTag( "Player" );
		players = new Character[p.Length]; int i = 0;
		foreach( GameObject go in p ) players[i++] = go.GetComponent<Character>();*/
		a = 0;
		StartCoroutine ("changePower");
	}

	void OnTriggerEnter (Collider collision)
	{
		if(collision.gameObject.tag == "Player"){

			Character col = collision.gameObject.GetComponent<Character>();

			if(col.PlayerInd == players[0].PlayerInd){
				Actor = players[0];
				ActedOn = players[1];
			}
			else{
				Actor = players[1];
				ActedOn = players[0];
			}
			StopSelector();
			transform.position = new Vector3(0,100,0);
		}
	}


	public void StopSelector()	
	{
		StopCoroutine ("changePower");
		audio.PlayOneShot(PickUpSound);
		if(a == 0)
		{
			Actor.SpeedBoost();
			Debug.Log ("Speed");
		}
		else if(a == 1)
		{
			ActedOn.InvertedControls();
			Debug.Log ("Invert");
		}
		else if(a == 2)
		{
			ActedOn.Invisibility();
			Debug.Log ("Invisible");
		}

	}

	IEnumerator changePower()
	{
		if(a == 3)
		{
			a=0; 
		}
		yield return new WaitForSeconds (waitTime);
		a++;
		StartCoroutine ("changePower");
	}
}
